/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Main.Toolbox;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Sik
 */
@Entity
@Table(name = "carte")
@XmlRootElement
@NamedQueries(
{
    @NamedQuery(name = "Carte.findAll", query = "SELECT c FROM Carte c ORDER BY DateDepot DESC"),
    @NamedQuery(name = "Carte.findByIdCarte", query = "SELECT c FROM Carte c WHERE c.idCarte = :idCarte"),
    @NamedQuery(name = "Carte.findByDesignation", query = "SELECT c FROM Carte c WHERE c.designation = :designation"),
    @NamedQuery(name = "Carte.findByDateDepot", query = "SELECT c FROM Carte c WHERE c.dateDepot = :dateDepot"),
    @NamedQuery(name = "Carte.findByDateExpiration", query = "SELECT c FROM Carte c WHERE c.dateExpiration = :dateExpiration"),
    @NamedQuery(name = "Carte.findByStatut", query = "SELECT c FROM Carte c WHERE c.statut = :statut")
})
public class Carte implements Serializable
{
    @Column(name =     "dateDepot")
    @Temporal(TemporalType.DATE)
    private Date dateDepot;
    @Column(name =     "dateExpiration")
    @Temporal(TemporalType.DATE)
    private Date dateExpiration;
    @Basic(optional = false)
    @Lob
    @Column(name = "path")
    private String path;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idCarte")
    private Integer idCarte;
    @Basic(optional = false)
    @Column(name = "designation")
    private String designation;
    @Column(name = "statut")
    private String statut;
    @JoinTable(name = "createurs_carte", joinColumns =
    {
        @JoinColumn(name = "Carte_idCarte", referencedColumnName = "idCarte")
    }, inverseJoinColumns =
    {
        @JoinColumn(name = "Utilisateur_idUtilisateur", referencedColumnName = "idUtilisateur")
    })
    @ManyToMany
    private List<Utilisateur> utilisateurList = new ArrayList<Utilisateur>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "carteidCarte")
    private List<Zone> zoneList = new ArrayList<Zone>();

    public Carte()
    {
    }

    public Carte(Integer idCarte)
    {
        this.idCarte = idCarte;
    }

    public Carte(Integer idCarte, String designation)
    {
        this.idCarte = idCarte;
        this.designation = designation;
    }

    public Integer getIdCarte()
    {
        return idCarte;
    }

    public void setIdCarte(Integer idCarte)
    {
        this.idCarte = idCarte;
    }

    public String getDesignation()
    {
        return designation;
    }

    public void setDesignation(String designation)
    {
        this.designation = designation;
    }

    public String getStatut()
    {
        return statut;
    }

    public void setStatut(String statut)
    {
        this.statut = statut;
    }

    @XmlTransient
    public List<Utilisateur> getUtilisateurList()
    {
        return utilisateurList;
    }

    public void setUtilisateurList(List<Utilisateur> utilisateurList)
    {
        this.utilisateurList = utilisateurList;
    }

    @XmlTransient
    public List<Zone> getZoneList()
    {
        return zoneList;
    }

    public void setZoneList(List<Zone> zoneList)
    {
        this.zoneList = zoneList;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (idCarte != null ? idCarte.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Carte))
        {
            return false;
        }
        Carte other = (Carte) object;
        if ((this.idCarte == null && other.idCarte != null) || (this.idCarte != null && !this.idCarte.equals(other.idCarte)))
        {
            return false;
        }
        return true;
    }
    
    public String xmlParse(boolean displayZones)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<carte>");
            sb.append("<idCarte>").append(this.getIdCarte()).append("</idCarte>");
            sb.append("<designation>").append(this.getDesignation()).append("</designation>");
            sb.append("<path>").append(this.path).append("</path>");
            String dateMax = "";
            if(dateExpiration != null)
            {
                dateMax = Toolbox.dateToString(this.getDateExpiration());
            }
            sb.append("<dateMax>").append(dateMax).append("</dateMax>");
            if(displayZones)
            {
                sb.append("<zones>");
                    if(zoneList != null)
                    {
                        for(Zone z : this.zoneList)
                        {
                            sb.append(z.xmlParse(true));
                        }
                    }
                sb.append("</zones>");
            }
        sb.append("</carte>");
        
        return sb.toString();
    }

    @Override
    public String toString()
    {
        return "Model.Carte[ idCarte=" + idCarte + " ]";
    }

    public String getPath()
    {
        return path;
    }

    public void setPath(String path)
    {
        this.path = path;
    }

    public Date getDateDepot()
    {
        return dateDepot;
    }

    public void setDateDepot(Date dateDepot)
    {
        this.dateDepot = dateDepot;
    }

    public Date getDateExpiration()
    {
        return dateExpiration;
    }

    public void setDateExpiration(Date dateExpiration)
    {
        this.dateExpiration = dateExpiration;
    }
    
}
